//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for ThreePanelResizeableControl.
	/// </summary>
	[ParseChildren(true)]
	public class ThreePanelResizeableControl : Control
	{
		#region Properties

		private Control _area1, _area2, _area3;
		public virtual Control Area1
		{
			get
			{
				return _area1;
			}
			set
			{
				_area1 = value;
				this.Controls.Add(_area1);
			}
		}

		public virtual Control Area2
		{
			get
			{
				return _area2;
			}
			set
			{
				_area2 = value;
				this.Controls.Add(_area2);
			}
		}

		public virtual Control Area3
		{
			get
			{
				return _area3;
			}
			set
			{
				_area3 = value;
				this.Controls.Add(_area3);
			}
		}

		public virtual string ResizeAreaCssClass
		{
			get
			{
				string state = (string) ViewState["ResizeAreaCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["ResizeAreaCssClass"] = value;
			}
		}

		public virtual string CollapseButtonCssClass
		{
			get
			{
				string state = (string) ViewState["CollapseButtonCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["CollapseButtonCssClass"] = value;
			}
		}

		public virtual string MoveLeftHtml
		{
			get
			{
				string state = (string) ViewState["MoveLeftHtml"];
				if (state == null)
					return "&lt;";
				else
					return state;
			}
			set
			{
				ViewState["MoveLeftHtml"] = value;
			}
		}

		public virtual string MoveRightHtml
		{
			get
			{
				string state = (string) ViewState["MoveRightHtml"];
				if (state == null)
					return "&gt;";
				else
					return state;
			}
			set
			{
				ViewState["MoveRightHtml"] = value;
			}
		}

		StringCollection _verticalDomElements = new StringCollection();
		public virtual StringCollection VerticalDomElements
		{
			get
			{
				return _verticalDomElements;
			}
		}

		public virtual bool FitToHeight
		{
			get
			{
				object state = ViewState["FitToHeight"];
				if (state == null)
					return false;
				else
					return (bool) state;
			}
			set
			{
				ViewState["FitToHeight"] = value;
			}
		}

		public virtual bool SaveStateToCookie
		{
			get
			{
				object state = ViewState["SaveStateToCookie"];
				if (state == null)
					return false;
				else
					return (bool) state;
			}
			set
			{
				ViewState["SaveStateToCookie"] = value;
			}
		}

		public virtual string CookieName
		{
			get
			{
				string state = (string) ViewState["CookieName"];
				if (state == null)
					return this.ClientID;
				else
					return state;
			}
			set
			{
				ViewState["CookieName"] = value;
			}
		}

		public virtual int HeightPercentage
		{
			get
			{
				object state = ViewState["HeightPercentage"];
				if (state == null)
					return 100;
				else
					return (int) state;
			}
			set
			{
				ViewState["HeightPercentage"] = value;
			}
		}

		public virtual int CookieDurationDays
		{
			get
			{
				object state = ViewState["CookieDurationDays"];
				if (state == null)
					return 31;
				else
					return (int) state;
			}
			set
			{
				ViewState["CookieDurationDays"] = value;
			}
		}

		public virtual int Area1DefaultWidthPercentage
		{
			get
			{
				object state = ViewState["Area1DefaultWidthPercentage"];
				if (state == null)
					return 33;
				else
					return (int) state;
			}
			set
			{
				ViewState["Area1DefaultWidthPercentage"] = value;
			}
		}

		public virtual int Area2DefaultWidthPercentage
		{
			get
			{
				object state = ViewState["Area2DefaultWidthPercentage"];
				if (state == null)
					return 33;
				else
					return (int) state;
			}
			set
			{
				ViewState["Area2DefaultWidthPercentage"] = value;
			}
		}

		public virtual int Area3DefaultWidthPercentage
		{
			get
			{
				object state = ViewState["Area3DefaultWidthPercentage"];
				if (state == null)
					return 33;
				else
					return (int) state;
			}
			set
			{
				ViewState["Area3DefaultWidthPercentage"] = value;
			}
		}

		#endregion

		#region Event Handlers

		protected override void OnPreRender(EventArgs e)
		{
			if (!this.Page.IsClientScriptBlockRegistered(this.GetType().FullName))
				this.Page.RegisterClientScriptBlock(this.GetType().FullName, string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",this.ResolveUrl("~/utility/ThreePanelResizeableControl.js")));

			StringBuilder script = new StringBuilder();
			script.Append("<script language=\"javascript\">var ");
			script.Append(this.ClientID);
			script.Append("=new ThreePanelResizeableControl('");
			script.Append(this.ClientID);
			script.Append("','");
			script.Append(this.ClientID);
			script.Append("','");
			script.Append(this.MoveLeftHtml.Replace("'", "\\'"));
			script.Append("','");
			script.Append(this.MoveRightHtml.Replace("'", "\\'"));
			script.Append("',new Array(");

			for (int i = 0; i < this.VerticalDomElements.Count; i++)
			{
				if (i > 0)
					script.Append(",");

				script.Append(this.VerticalDomElements[i]);				
			}

			script.Append("),");
			script.Append(this.FitToHeight ? (this.HeightPercentage / 100.0f).ToString() : "null");
			script.Append(",'");
			script.Append(this.SaveStateToCookie ? this.CookieName : "");
			script.Append("',");
			script.Append(this.CookieDurationDays.ToString());
			script.Append(", new Array(");
			script.Append((this.Area1DefaultWidthPercentage / 100.0f).ToString());
			script.Append(",");
			script.Append((this.Area2DefaultWidthPercentage / 100.0f).ToString());
			script.Append(",");
			script.Append((this.Area3DefaultWidthPercentage / 100.0f).ToString());
			script.Append("));</script>");

			this.Page.RegisterStartupScript(this.ClientID, script.ToString());		

			base.OnPreRender(e);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			writer.Write("<div id=\"");
			writer.Write(this.ClientID);
			writer.Write("\" style=\"width: 100%; overflow: hidden; padding: 0px; margin: 0px; border-width: 0px;\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"empty-cells: show;\"><tr valign=\"top\"><td><div style=\"overflow: hidden;\">");

			// render area 1
			if (this.Area1 != null)
				this.Area1.RenderControl(writer);
	
			writer.Write("</div></td><td valign=\"middle\" class=\"");
			writer.Write(this.ResizeAreaCssClass);
			writer.Write("\" onmousedown=\"");
			writer.Write(this.ClientID);
			writer.Write(".StartResizing(this, event);\"><a href=\"#\" class=\"");
			writer.Write(this.CollapseButtonCssClass);
			writer.Write("\" onclick=\"");
			writer.Write(this.ClientID);
			writer.Write(".ToggleCollapse(this, 'left'); return false;\">");
			writer.Write(this.MoveLeftHtml);
			writer.Write("</a><a href=\"#\" class=\"");
			writer.Write(this.CollapseButtonCssClass);
			writer.Write("\" onclick=\"");
			writer.Write(this.ClientID);
			writer.Write(".ToggleCollapse(this, 'right'); return false;\">");
			writer.Write(this.MoveLeftHtml);
			writer.Write("</a></td><td><div style=\"overflow: hidden;\">");

			// render area 2
			if (this.Area2 != null)
				this.Area2.RenderControl(writer);

			writer.Write("</div></td><td valign=\"middle\" class=\"");
			writer.Write(this.ResizeAreaCssClass);
			writer.Write("\" onmousedown=\"");
			writer.Write(this.ClientID);
			writer.Write(".StartResizing(this, event);\"><a href=\"#\" class=\"");
			writer.Write(this.CollapseButtonCssClass);
			writer.Write("\" onclick=\"");
			writer.Write(this.ClientID);
			writer.Write(".ToggleCollapse(this, 'left'); return false;\">");
			writer.Write(this.MoveRightHtml);
			writer.Write("</a><a href=\"#\" class=\"");
			writer.Write(this.CollapseButtonCssClass);
			writer.Write("\" onclick=\"");
			writer.Write(this.ClientID);
			writer.Write(".ToggleCollapse(this, 'right'); return false;\">");
			writer.Write(this.MoveLeftHtml);
			writer.Write("</a></td><td><div style=\"overflow: hidden;\">");
	
			// render area 3
			if (this.Area3 != null)
				this.Area3.RenderControl(writer);
		
			writer.Write("</div></td></tr></table></div>");
		}

		#endregion
	}
}
